package com.zxy.ziems.server.device.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.alarm.enums.AlarmTypeParamEnum;
import com.zxy.ziems.server.classify.pojo.param.ClassifyMeasureDataHourParam;
import com.zxy.ziems.server.device.pojo.dto.*;
import com.zxy.ziems.server.device.pojo.entity.DeviceEntity;
import com.zxy.ziems.server.device.pojo.param.DeviceQueryParam;
import com.zxy.ziems.server.device.pojo.vo.DeviceAlarmVO;
import com.zxy.ziems.server.device.pojo.vo.DeviceMeasureDataHourVO;
import com.zxy.ziems.server.device.pojo.vo.DeviceVO;
import com.zxy.ziems.server.device.service.DeviceService;
import com.zxy.ziems.server.dto.DataQueryDto;
import com.zxy.ziems.server.energy.mapper.DeviceMeasureDataDayMapper;
import com.zxy.ziems.server.energy.service.DeviceMeasureDataDayService;
import com.zxy.ziems.server.energy.service.DeviceMeasureDataIncrementHourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 采集管理-设备管理
 *
 * @author chdzq
 * @create 2023/3/22
 */
@RestController
@Validated
@RequestMapping("/api")
public class DeviceController {

    @Autowired
    private DeviceMeasureDataIncrementHourService deviceMeasureDataIncrementHourService;

    @Autowired
    private DeviceMeasureDataDayService deviceMeasureDataDayService;

    /**
     * 新增设备
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/device/manage/add")
    public void add(@Validated(value = DeviceDTO.Add.class) @RequestBody DeviceDTO<Object> dto) throws Exception {
        service.add(dto);
    }


    /**
     * 新增设备(只添加设备名称，设备编码，设备类型基础信息)
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/device/manage/addBase")
    public void addBase(@Validated(value = DeviceBaseDTO.Add.class) @RequestBody DeviceBaseDTO<Object> dto) throws Exception {
        service.addBase(dto);
    }

    /**
     * 更新设备
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/device/manage/update")
    public void update(@Validated(value = DeviceDTO.Update.class) @RequestBody DeviceDTO<Object> dto) throws Exception {
        service.update(dto);
    }

    /**
     * 删除设备
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/device/manage/delete")
    public void delete(@Validated(value = DeviceDTO.Select.class) @RequestBody DeviceDTO<Object> dto) throws Exception {
        service.deleteById(dto.getId());
    }

    /**
     * 查询设备信息
     *
     * @param dto 用户post参数
     * @return
     */
    @GetMapping("/device/manage/detail")
    public DeviceVO<Object> detail(@Validated(value = DeviceDTO.Select.class) DeviceDTO<Object> dto) throws Exception {
        return service.viewById(dto.getId());
    }

    /**
     * 分页查询设备
     *
     * @return
     */
    @GetMapping("/device/manage/page")
    public PageResult<DeviceVO<Object>> page(@Validated DeviceQueryParam param) throws Exception {
        return service.pageListBy(param);
    }

    /**
     * 查询设备List
     *
     * @return
     */
    @GetMapping("/device/manage/list")
    public List<DeviceVO<Object>> list(DeviceQueryParam param) throws Exception {
        return service.listByCodeList(param.getType(), param.getName());
    }

    /**
     * 设备列表树
     *
     * @return
     */
    @GetMapping("/device/manage/tree")
    public List<DeviceTreeVO> tree() throws Exception {
        return service.tree();
    }

    /**
     * 设备列表树
     *
     * @return
     */
    @GetMapping("/device/manage/initTrim")
    public void initTrim() throws Exception {
        List<DeviceEntity> list = service.list();
        for (DeviceEntity deviceEntity : list) {
            deviceEntity.setCode(deviceEntity.getCode().trim());
            deviceEntity.setName(deviceEntity.getName().trim());
        }
        service.saveOrUpdateById(list);

    }

    /**
     * 安全检测点位告警
     *
     * @param alarmSceneDeviceDTO
     * @return
     */
    @GetMapping("/device/manage/alarm")
    public List<DeviceAlarmVO> getListAddressAndAlarm(@Validated(value = AlarmSceneDeviceDTO.Select.class) AlarmSceneDeviceDTO alarmSceneDeviceDTO) {
        return service.listAddressAndAlarm(alarmSceneDeviceDTO.getSentId(), AlarmTypeParamEnum.GAS.getCode());
    }

    /**
     * 数据修正列表（作废）
     * @param param
     * @return
     */
//    @GetMapping("/getMeasureDataHour")
//    public PageResult<DeviceMeasureDataHourVO> getMeasureDataHour(@Validated ClassifyMeasureDataHourParam param) {
//        return deviceMeasureDataIncrementHourService.getDeviceMeasureDataHour(param);
//    }

    /**
     * 数据修正列表
     *
     * @param param
     * @return
     */
    @GetMapping("/getMeasureData")
    public PageResult<DeviceMeasureDataHourVO> getMeasureData(@Validated ClassifyMeasureDataHourParam param) {
        return deviceMeasureDataIncrementHourService.getMeasureData(param);
    }

    /**
     * 数据修正
     * @param dto
     * @return
     */
//    @PostMapping("modifyMeasureDataHour")
//    public JsonResponse<Void> modifyMeasureDataHour(@Validated @RequestBody DeviceMeasureDataHourDTO dto) {
//        deviceMeasureDataIncrementHourService.modifyMeasureDataHour(dto);
//        return JsonResponse.SUCCESS();
//    }

    /**
     * 数据修正
     *
     * @param dtos
     * @return
     */
    @PostMapping("/modifyMeasureData")
    public JsonResponse<Void> modifyMeasureData(@Validated @RequestBody List<DeviceMeasureDataHourDTO> dtos) {
        deviceMeasureDataIncrementHourService.modifyMeasureData(dtos);
        return JsonResponse.SUCCESS();
    }


    /**
     * 批量修正小时采集表值
     * 按照需要修正的小时点数2025-03-06 09:00，批量修正小时数据值
     * 用于history表中有历史数据，根据历史数据对小时数据进行修正
     * @param dateTimeStr
     * @return
     */
    @GetMapping("/modifyHourData")
    public JsonResponse<Void> modifyHourData(@Validated String dateTimeStr) {
        deviceMeasureDataIncrementHourService.modifyHourData(dateTimeStr);
        return JsonResponse.SUCCESS();
    }

    /**
     * 临时处理3月7~10号日级别异常数据接口
     * TODO：根据dataTemp表临时处理天数据，该接口可拓展为手动修正天数据接口
     * 没有考虑每天的平谷分段，直接取的每天电量价格，如果电科设置平谷分段需要完善。
     */
    @GetMapping("/DataProcessing")
    public void DataProcessing(){
        deviceMeasureDataDayService.dataProcessing();
    }

    /**
     * 临时处理天级别数据异常，3月18日实施采集值记录到了增量值
     */
    @GetMapping("/dataProcessingTemp")
    public void dataProcessingTemp(@Validated String date){
        deviceMeasureDataDayService.dataProcessingTemp(date);
    }


    /**
     * 临时处理天级别数据异常，3月17数据异常
     */
    @GetMapping("/modifyDayData")
    public void modifyDayData(@Validated String date){
        deviceMeasureDataDayService.modifyDayData(date);
    }

    /**
     * 初始化电力设备
     */
    @PostMapping("/initDlDevice")
    public JsonResponse<Void> initDlDevice() {
        service.initDlDevice();
        return JsonResponse.SUCCESS();
    }

    /**
     * 批量创建设备
     */
    @PostMapping("/batchAddDevice")
    public JsonResponse<Void> batchAddDevice(@RequestBody BatchAddDevicePropertyDTO dto) {
        service.batchAddDevice(dto);
        return JsonResponse.SUCCESS();
    }


    private DeviceService service;

    @Autowired
    public void setService(DeviceService deviceService) {
        this.service = deviceService;
    }
}
